package com.dartsstats.datasource;

import java.util.ArrayList;
import java.util.List;

import com.dartsstats.Team;
import com.dartsstats.DM.TeamDM;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class TeamDataSource {
	
	private SQLiteDatabase database;
	private TeamDM teamDM;
	private String[] allColumns = { TeamDM.COLUMN_ID, TeamDM.COLUMN_NAME, TeamDM.COLUMN_ISMANAGED,};
	
	public TeamDataSource(Context context) {
		teamDM = new TeamDM(context);
	}
	
	public void open() throws SQLException {
		database = teamDM.getWritableDatabase();
	}
	
	public void close() {
		teamDM.close();
	}
	
	public Team createTeam(String name, boolean isManaged){
		openDatabaseIfNull();
		ContentValues values = new ContentValues();
		values.put(TeamDM.COLUMN_NAME, name);
		values.put(TeamDM.COLUMN_ISMANAGED, String.valueOf(isManaged));
		long insertId = database.insert(TeamDM.TABLE_TEAM, null, values);
		Cursor cursor = database.query(TeamDM.TABLE_TEAM, allColumns, TeamDM.COLUMN_ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		Team team = cursorToTeam(cursor);
		cursor.close();
		return team;
	}
	
	public void deleteTeam(Team team) {
		openDatabaseIfNull();
		long id = team.getId();
		System.out.println("Team deleted with id: " + id);
		database.delete(TeamDM.TABLE_TEAM, TeamDM.COLUMN_ID + " = " + id, null);
	}
	
	public List<Team> getAllTeams() {
		openDatabaseIfNull();
		List<Team> teams = new ArrayList<Team>();
		Cursor cursor = database.query(TeamDM.TABLE_TEAM, allColumns, null, null, null, null, null);
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			Team team = cursorToTeam(cursor);
			teams.add(team);
			cursor.moveToNext();
		}
		cursor.close();
		return teams;
	}
	
	public Team getTeamByID(int teamID) {
		openDatabaseIfNull();
		String[] whereArgs = new String[] {String.valueOf(teamID)};
		Cursor cursor = database.query(TeamDM.TABLE_TEAM, allColumns, TeamDM.COLUMN_ID + " = ?", whereArgs, null, null, null);
		cursor.moveToFirst();
		Team team = cursorToTeam(cursor);
		cursor.close();
		return team;
	}
	
	public Team getTeamByName(String teamName) {
		openDatabaseIfNull();
		String[] whereArgs = new String[] {teamName};
		Cursor cursor = database.query(TeamDM.TABLE_TEAM, allColumns, TeamDM.COLUMN_NAME + " = ?", whereArgs, null, null, null);
		cursor.moveToFirst();
		Team team = cursorToTeam(cursor);
		cursor.close();
		return team;
	}
	
	private Team cursorToTeam(Cursor cursor) {
		Team team = new Team();
		team.setId(cursor.getLong(0));
		team.setName(cursor.getString(1));
		team.setManaged(cursor.getString(2));
		return team;
	}
	
	private void openDatabaseIfNull(){
		if(database==null){
			open();
		}
	}

	public void updateTeamName(String string, boolean isManaged, long teamID) {
		openDatabaseIfNull();
		ContentValues values = new ContentValues();
		values.put(TeamDM.COLUMN_NAME, string);
		values.put(TeamDM.COLUMN_ISMANAGED, isManaged);
		String[] whereArgs = new String[] {String.valueOf(teamID)};
		database.update(TeamDM.TABLE_TEAM, values, TeamDM.COLUMN_ID + " = ?", whereArgs);
		
	}

}
